package com.hongning.railway.binding.edittext;


import android.databinding.BindingAdapter;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import me.goldze.mvvmhabit.utils.ToastUtils;


/**
 * Created by goldze on 2017/6/16.
 * TwinklingRefreshLayout列表刷新的绑定适配器
 */
public class ViewAdapter {
    @BindingAdapter(value = {"inputfilter"}, requireAll = false)
    public static void setInputfilter(EditText textView, final Boolean inputfilter) {

        TextWatcher textWatcher = new TextWatcher() {

            private String outStr = ""; //这个值存储输入超过两位数时候显示的内容

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String edit=s.toString();
                if (edit.length()==2&&Integer.parseInt(edit)>=10){
                    outStr=edit;
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String words = s.toString();
                //首先内容进行非空判断，空内容（""和null）不处理
                if (!TextUtils.isEmpty(words)) {
                    //1-100的正则验证
                    Pattern p = Pattern.compile("^(100|[1-9]\\d|\\d)$");
                    Matcher m = p.matcher(words);
                    if (m.find() || ("").equals(words)) {
                        //这个时候输入的是合法范围内的值
                    } else {
                        if (words.length() > 2) {
                            //若输入不合规，且长度超过2位，继续输入只显示之前存储的outStr
                            textView.setText(outStr);
                            //重置输入框内容后默认光标位置会回到索引0的地方，要改变光标位置
                            textView.setSelection(2);
//                    }
                            ToastUtils.showShort("请输入范围在1-100之间的整数");
                        }
                    }
                }
            }


            @Override
            public void afterTextChanged(Editable s) {
                //这里的处理是不让输入0开头的值
                String words = s.toString();
                //首先内容进行非空判断，空内容（""和null）不处理
                if (!TextUtils.isEmpty(words)) {
                    if (Integer.parseInt(s.toString()) <= 0) {
                        textView.setText("");
                        ToastUtils.showShort("请输入范围在1-100之间的整数");
                    }
                }
            }
        };
        if (inputfilter){
            textView.addTextChangedListener(textWatcher);
        }

    }
}
